package com.shuoyue.adven.init;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.shuoyue.adven.Constant;
import com.shuoyue.adven.MySpUtils;
import com.shuoyue.adven.R;
import com.shuoyue.adven.entity.AppVersionInfo;
import com.shuoyue.adven.entity.Settings;
import com.shuoyue.adven.main.TestActivity;
import com.shuoyue.adven.main.appcontrol.PowerUtil;
import com.shuoyue.adven.main.bussiness.MainFragmentActivity;
import com.sm.appbase.base.basemvp.BaseMvpActivity;
import com.sm.appbase.net.exception.NetErrorException;

import java.io.File;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class LauncherActivity extends BaseMvpActivity<AppSettingContract.Presenter>
        implements EasyPermissions.PermissionCallbacks, AppSettingContract.View {
    String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    String PERMISSION_STORAGE_MSG = "请授予应用存储权限，否则无法使用";
    int PERMISSION_STORAGE_CODE = 10001;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_launcher;
    }

    @Override
    protected void initView() {

    }

    @Override
    public void initData() {

        String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(mContext, permissions)) {

        } else {
            EasyPermissions.requestPermissions(this, "需要获取您的存储使用权限", 1, permissions);
        }

        mPresenter = new AppSettingContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getSettings();
    }

    private void goNext() {
        if (TextUtils.isEmpty(MySpUtils.getToken(mContext))) {
            new Handler().postDelayed(() -> {
                startActivity(new Intent(mContext, RegistActivity.class));
                finish();
            }, 2000);
        } else {
            new Handler().postDelayed(() -> {
//                    startActivity(new Intent(mContext, TestActivity.class));
                startActivity(new Intent(this, MainFragmentActivity.class));
                finish();
            }, 2000);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        initView();
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        finish();
    }


    @Override
    public void getSettingSuc(Settings settings) {
        if (settings == null) {
            return;
        }
        if (settings.getRollPixel() != 0) {
            Constant.MAIN_SCROLL_SPEED = settings.getRollPixel();
        }
        if (settings.getRollFrameRate() != 0) {
            Constant.main_SCROLL_FRAM = settings.getRollFrameRate();
        }
        if (settings.getScreenSwitchTime() != 0) {
            Constant.IMPOTENT_SCROLL_DELAY = settings.getScreenSwitchTime();
        }
        if (settings.getRegionSwitchTime() != 0) {
            Constant.SUB_SCREEN_SCROLL_DELAY = settings.getRegionSwitchTime();
        }
        goNext();
    }

    @Override
    public void onError(NetErrorException throwable) {
        super.onError(throwable);
        goNext();
    }
}
